package com.wp.rcm.controller;
import com.wp.rcm.pojo.Chance;
import com.wp.rcm.pojo.Lost;
import com.wp.rcm.service.LostService;
import com.wp.rcm.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/crm/lost")
public class LostController{

    @Resource
    private LostService lostService;

    @RequestMapping("/list")
    public String findAll(Model model, @RequestParam(required = false)String lstCustName,
                          @RequestParam(required = false)String lstCustManagerName,Integer currPageNo){

        Integer page1 = currPageNo==null?1:currPageNo;

        Page page = new Page();
        page.setTotal(lostService.count(lstCustManagerName,lstCustName)); // 数据总条数
        if (page1<1){
            page1=1;
            model.addAttribute("message","当前已是最顶一页");
        }
        if (page1>page.getPages()){
            page1=page.getPages();
            model.addAttribute("message","当前已是最后一页");
        }
        page.setCurrent(page1); // 设置偏移量

        // 存储数据
        List<Lost> all = lostService.findAll(lstCustName, lstCustManagerName, page.getOffset(), page.getPageSize());
        page.setList(all);

        model.addAttribute("list",page);


        return "cstLost/list";
    }

    // 修改数据
    @RequestMapping("/edit")
    public String edit(Integer lstId,Model model){

        System.out.println("-------------"+lstId);

        Lost lost = lostService.findByLstId(lstId);

        model.addAttribute("lost",lost);

        return "cstLost/edit";
    }

    @PostMapping("/save")
    public String upd(Lost lost){

        int upd = lostService.update(lost);

        if (upd>0){
            return "redirect:/crm/lost/list";
        }
        return "redirect:/crm/lost/edit";
    }
}
